package com.fangyi.yunmusic.config;

import com.fangyi.yunmusic.interceptor.LoginInterceptor;
import com.fangyi.yunmusic.interceptor.UserOpInterceptor;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Configuration
public class WebMvcConfig  implements WebMvcConfigurer {


    // 处理跨域问题
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("POST", "GET", "OPTIONS", "HEAD", "DELETE", "PUT")
                .allowCredentials(true)
                .allowedHeaders("*", "userId", "userToken");
    }

    // 添加静态资源路径
//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/**")
//                .addResourceLocations("file:" + "D:/yunmusic_dev" + "/");
//    }

    @Bean
    public LoginInterceptor loginInterceptor() {
        return new LoginInterceptor();
    }

    @Bean
    public UserOpInterceptor userOpInterceptor() {
        return new UserOpInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor())
                .addPathPatterns("/comment/publish", "/comment/delete", "/commentLike", "/logout", "/checkToken", "/playlist/add", "/playlist/delete");

        registry.addInterceptor(userOpInterceptor())
                .addPathPatterns("/playlist/add", "/playlist/delete");
    }
}
